﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Citi.ReportManager.Domain;
using Citi.ReportManager.WebUi.Models;
using Citi.ReportManager.WebUi.Utils;
using System.IO;

namespace Citi.ReportManager.WebUi.Controllers
{
    public class ReportRequestController : Controller
    {
        public ActionResult MostDownloaded()
        {
            var requests = new HightlightedReport().SelectMostDownload(20);
            return GetHighlightedPartialView(requests, false);
        }

        public ActionResult RecentlyAdded()
        {
            var requests = new HightlightedReport().SelectRecentlyAdded(20);
            return GetHighlightedPartialView(requests, false);
        }

        public ActionResult DownloadedRecent()
        {
            var requests = new HightlightedReport().SelectDownloadedRecent(20);
            return GetHighlightedPartialView(requests, false);
        }

        public ActionResult MyReports()
        {
            var requests = new HightlightedReport().SelectDownloadedRecent(50);

            var models = new HighlightedReportModel().CreateModel(requests).Where(x => x.Valid).ToList();
            models.ForEach(x =>
            {
                var reportName = !string.IsNullOrWhiteSpace(Path.GetExtension(Path.GetFileNameWithoutExtension(x.ReportFullPath)))
                        ? Path.GetFileNameWithoutExtension(x.ReportFullPath)
                        : x.ReportFullPath;

                x.Icon = IconHelper.GetFileIcon(reportName, Server.MapPath("~/Content/Images/Files"));
            });

            return View(models);
        }

        public ActionResult MyRecentDownloads()
        {
            var requests = new HightlightedReport().SelectDownloadedRecent(20);
            return GetHighlightedPartialView(requests, true);
        }

        private ActionResult GetHighlightedPartialView(IEnumerable<HightlightedReport> requests, bool showRequestDate)
        {
            var models = new HighlightedReportModel().CreateModel(requests).Where(x => x.Valid).Take(5).ToList();
            models.ForEach(x =>
            {
                var reportName = !string.IsNullOrWhiteSpace(Path.GetExtension(Path.GetFileNameWithoutExtension(x.ReportFullPath)))
                      ? Path.GetFileNameWithoutExtension(x.ReportFullPath)
                      : x.ReportFullPath;

                x.Icon = IconHelper.GetFileIcon(reportName, Server.MapPath("~/Content/Images/Files"));
                x.ShowRequestDate = showRequestDate;
            });
            return PartialView("Highlighted", models);
        }

        public ActionResult CategoriesMostRequested()
        {
            var requests = new HightlightedReport().SelectCategoryMostDownloadByUser(5, User.Identity.Name.Split(new[] { '\\' }).Last());
            var models = new HighlightedReportModel().CreateModel(requests);
            return PartialView(models);
        }

        public ActionResult FavoriteCategories(int takeNumber )
        {
            var requests = new HightlightedReport().SelectFavorites(takeNumber, User.Identity.Name.Split(new[] { '\\' }).Last());
            var models = new HighlightedReportModel().CreateModel(requests);
            return PartialView("CategoriesMostRequested", models);
        }

        public ActionResult MyFavorites()
        {
            return View();
        }
    }
}
